<template>
	<view class="bg-gray" style="height: auto;">
		<cu-custom bgColor="bg-informatic-brown" isBack="../index/index">
			<block slot="backText">返回</block>
			<block slot="content">个人简历</block>
			<block slot="right">
				<view class="padding-right-xs" @click="getCXWData()">[同步创新网数据]</view>
			</block>
		</cu-custom>
		<view class="solids">
			<panel component="profile" :data="{}"></panel>
			<panel component="edu" :data="{}"></panel>
			<panel component="project" :data="project"></panel>
			<panel component="award" :data="award"></panel>
			<panel component="memo" :data="{}"></panel>
		</view>
		<view>
			<view class="panel bg-white shadow shadow-lg text-lg">
				<picker mode="selector" :range="templates" @change="selectTemplate" :value="currentTemplate">
					当前简历样式：{{templates[currentTemplate]}}
				</picker>
			</view>
		</view>
		<view>
			<view class="panel bg-white shadow shadow-lg">
				<button class="cu-btn bg-blue" style="width: 100%;" :disabled="disabled"
					@click="sendEmailBtn()">发送简历到邮箱{{btnCount}}</button>
			</view>
		</view>
	</view>
</template>

<script>
	import panel from './components/edit/panel.vue';
	export default {
		components: {
			panel
		},
		data() {
			return {
				project: [],
				award: [],
				btnCount: "",
				disabled: false,
				timer: '',
				templates: ['默认模板', '模板1'],
				templateName: ['default_panel', 'template_1'],
				currentTemplate: 0
			}
		},
		methods: {
			getCXWData() {
				uni.showLoading();
				uni.post("/api/resume/GetResumeFromSchool", {}, msg => {
					uni.hideLoading();
					if (msg.success) {
						uni.showToast({
							title: '同步完成'
						});
						var data = [];
						for (let i = 0; i < msg.exp.length; i++) {
							data.push({
								start: msg.exp[i].BeginTime,
								end: msg.exp[i].EndTime,
								name: msg.exp[i].Name,
								organization: msg.exp[i].DepartName,
								detail: msg.exp[i].Description,
								id: '',
								job: msg.exp[i].Position,
								new: true
							});
						}
						this.project = [];
						this.project = data;
						data = [];
						for (let i = 0; i < msg.awards.length; i++) {
							data.push({
								start: msg.awards[i].BeginTime,
								end: msg.awards[i].EndTime,
								name: msg.awards[i].Name,
								organization: msg.awards[i].DepartName,
								detail: msg.awards[i].Description,
								id: '',
								job: msg.awards[i].Position,
								new: true
							});
						}
						this.award = [];
						this.award = data;
					} else {
						uni.showToast({
							title: msg.msg,
							icon: 'none'
						});
					}
				});
			},
			sendEmail() {
				uni.showLoading();
				uni.post("/api/resume/sendresume?show=email", {
					template: 'table'
				}, msg => {
					this.disabled = true;
					uni.hideLoading();
					if (!msg.success) {
						uni.showToast({
							title: msg.msg
						});
					} else {
						uni.showToast({
							title: '已发送'
						});
					}
				});
			},
			back() {
				uni.navigateTo({
					url: "../index/index"
				});
			},
			sendEmailBtn() {
				if (!this.disabled) {
					this.disabled = true;
					this.btnCount = "(一分钟内仅可发送一次)"
					this.timer = setTimeout(() => {
						this.disabled = false;
						this.btnCount = "";
					}, 60 * 1000);
					this.sendEmail();
				}
			},
			selectTemplate(e) {
				this.currentTemplate = e.detail.value;
				uni.showLoading();
				uni.post("/api/resume/SaveTemplate",{Template: this.templateName[this.currentTemplate]},msg => {
					uni.hideLoading();
					if (!msg.success) {
						uni.showToast({
							icon: 'none',
							title: msg.msg
						});
					} else {
						uni.showToast({
							title: '修改成功'
						});
					}
				});
			}
		},
		created() {
			uni.showLoading();
			uni.post("/api/resume/GetResumeTemplate", {}, msg => {
				uni.hideLoading();
				if (msg.success) {
					// this.resumeData = msg;
					var template = msg.data;
					if (template == '' || !template) {
						this.currentTemplate = 0;
					} else {
						for (let i = 0; i < this.templateName.length; i++) {
							if (template == this.templateName[i]) {
								this.currentTemplate = i;
								break;
							}
						}
					}
				} else {
					uni.showToast({
						title: msg.msg,
						icon: 'none'
					});
				}
			});
		}
	}
</script>

<style>
	.panel {
		/* width: 100%; */
		margin: 10px;
		padding: 10px;
		height: auto;
	}

	.panel-header {
		display: flex;
		flex-direction: row;
		align-items: baseline;
		justify-content: space-between;
	}

	.word-warp {
		word-wrap: break-word;
		word-break: break-all;
		overflow: hidden;
	}
</style>